package vip.liux.backend.infrastructure.jpa.pay;

import jakarta.persistence.criteria.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import vip.liux.contracts.models.pay.PayWalletTransaction;
import vip.liux.contracts.repositories.pay.PayWalletTransactionRepository;

import java.util.ArrayList;
import java.util.List;

public interface PayWalletTransactionJpaRepository extends JpaRepository<PayWalletTransaction, Long>, PayWalletTransactionRepository, JpaSpecificationExecutor<PayWalletTransaction> {

    @Override
    default Page<PayWalletTransaction> findPage(Long walletId, String uid, Pageable page) {
        return findAll((root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (StringUtils.isNotBlank(uid)) {
                predicates.add(criteriaBuilder.equal(root.get("uid"), uid));
            }
            if (walletId != null) {
                predicates.add(criteriaBuilder.equal(root.get("walletId"), walletId));
            }
            return query.where(predicates.toArray(new Predicate[0])).getRestriction();
        }, page);
    }
}